<!-- 运营审批详情 -->

<template>
    <div class="approve-detail">
        <div v-if="showPage == 0" class="empty">
            <div v-if="detail.loading" style="position: fixed;top: 50%;left: 50%;transform: translate(-50%,-50%);">
                <van-loading type="spinner" />
            </div>
        </div>
        <div v-else-if="showPage == 1" class="unbound-tip">
            <div>您暂未绑定账户,</div>
            <div>请联系公司管理员扫码绑定！</div>
        </div>
        <div v-else-if="showPage == 2" class="unbound-tip">
            <div>您当前绑定多个账户,</div>
            <div>请联系公司管理员！</div>
        </div>

        <div v-else class="approve-detail-container">
            <div v-if="detail.loading" style="position: fixed;top: 50%;left: 50%;transform: translate(-50%,-50%);">
                <van-loading type="spinner" />
            </div>
            <div v-else>
                <div class="info">
                    <div class="row">
                        <div class="left">审批单号</div>
                        <div class="right"><span>{{ detail.mainInfo.approveNo }}</span></div>
                    </div>
                    <div class="row">
                        <div class="left">类型</div>
                        <div class="right"><span>{{ detail.mainInfo.bizTypeName }}</span></div>
                    </div>
                    <div class="row">
                        <div class="left">客户</div>
                        <div class="right"><span>{{ detail.mainInfo.allname }}</span></div>
                    </div>
                    <div class="row">
                        <div class="left">发起人</div>
                        <div class="right"><span>{{ detail.mainInfo.createBy }}</span></div>
                    </div>
                    <div class="row">
                        <div class="left">件数</div>
                        <div class="right"><span>{{ detail.mainInfo.spjs }}件</span></div>
                    </div>
                    <div class="row">
                        <div class="left">重量</div>
                        <div class="right"><span>{{ detail.mainInfo.spsl }}吨</span></div>
                    </div>
                    <div class="row">
                        <div class="left">仓库</div>
                        <div class="right"><span>{{ detail.mainInfo.spckmc || '--' }}</span></div>
                    </div>
                    <div class="row">
                        <div class="left">业务日期</div>
                        <div class="right"><span>{{ detail.mainInfo.rqid || '--' }}</span></div>
                    </div>
                    <div class="row">
                        <div class="left">状态</div>
                        <div class="right"><span><span>{{ detail.mainInfo.statusName || '--' }}</span></span></div>
                    </div>
                </div>
                <!-- 普通出仓 -->
                <div v-if="layouType == 1">
                    <div class="info">
                        <div class="row">
                            <div class="left">车牌</div>
                            <div class="right"><span>{{ detail.info.dzzy6 || '--' }}</span></div>
                        </div>
                        <div class="row">
                            <div class="left">司机</div>
                            <div class="right"><span>{{ detail.info.dzzy7 || '--' }}</span></div>
                        </div>
                        <div class="row">
                            <div class="left">出仓单号</div>
                            <div class="right"><span>{{ detail.info.pzbh || '--' }}</span></div>
                        </div>
                        <div class="row">
                            <div class="left">出仓日期</div>
                            <div class="right"><span>{{ detail.info.outDate || '--' }}</span></div>
                        </div>
                    </div>
                    <div class="list" v-if="detail.list.length">
                        <div v-for="(item, index) in detail.list" :key="item.id">
                            <div class="title display-flex justify-between">
                                <span v-if="index === 0" class="text-bold font-size-26">货物信息</span>
                                <div class="text-center flex-item font-size-26"
                                    :style="index === 0 ? 'margin-left:-50px' : ''">
                                    {{
            index + 1 }}/{{ detail.list.length }}</div>
                            </div>
                            <div class="item">
                                <div class="row">
                                    <div class="left">物品规格</div>
                                    <div class="right"><span>{{ item.spzs || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">品牌</div>
                                    <div class="right"><span>{{ item.spcd || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">批号</div>
                                    <div class="right"><span>{{ item.spbh || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">柜号</div>
                                    <div class="right"><span>{{ item.spgh || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">件数</div>
                                    <div class="right"><span>{{ item.spjs }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">单重（KG）</div>
                                    <div class="right"><span>{{ item.spjsl }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">重量（吨）</div>
                                    <div class="right"><span>{{ item.spsl }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">业务合同</div>
                                    <div class="right"><span class="red-font">{{ item.contractNo || '--' }}</span></div>
                                </div>
                                <div class="row" v-if="detail.mainInfo.bizType == 'FOREIGN_OUT'">
                                    <div class="left">估值单价</div>
                                    <div class="right"><span>{{ item.price }}</span>元/吨</div>
                                </div>
                                <div class="row" v-if="detail.mainInfo.bizType == 'FOREIGN_OUT'">
                                    <div class="left">估值总额</div>
                                    <div class="right"><span>{{ item.spje }}</span>元</div>
                                </div>
                                <div class="row">
                                    <div class="left">条码备注</div>
                                    <div class="right"><span class="red-font">{{ item.itbz || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">货物条码</div>
                                    <div class="right"><span>{{ item.sptm }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">库存类型</div>
                                    <div class="right"><span>{{ item.spdm }}</span></div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- 普通转货 -->
                <div v-if="layouType == 2">
                    <div class="info">
                        <div class="row">
                            <div class="left">旧货权人</div>
                            <div class="right"><span>{{ detail.info.khmcold }}</span></div>
                        </div>
                        <div class="row">
                            <div class="left">新货权人</div>
                            <div class="right"><span>{{ detail.info.khmcnew }}</span></div>
                        </div>
                        <div class="row">
                            <div class="left">转货费方</div>
                            <div class="right"><span>{{ detail.info.hqf2 == '1' ? '新货权方' :
            detail.info.hqf2 == '0' ? '旧货权方' : detail.info.hqf2 == '-1' ? '免收费用' : '--'
                                    }}</span>
                            </div>
                        </div>
                        <div class="row">
                            <div class="left">货转单号</div>
                            <div class="right"><span>{{ detail.info.pzbh }}</span></div>
                        </div>
                        <div class="row">
                            <div class="left">货转日期</div>
                            <div class="right"><span>{{ detail.info.ssrqid }}</span></div>
                        </div>
                    </div>
                    <div class="list" v-if="detail.list.length">
                        <div v-for="(item, index) in detail.list" :key="item.id">
                            <div class="title display-flex justify-between">
                                <span v-if="index === 0" class="text-bold font-size-26">货物信息</span>
                                <div class="text-center flex-item font-size-26"
                                    :style="index === 0 ? 'margin-left:-50px' : ''">
                                    {{ index + 1 }}/{{ detail.list.length }}</div>
                            </div>
                            <div class="item">
                                <div class="row">
                                    <div class="left">物品规格</div>
                                    <div class="right"><span>{{ item.spzs || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">品牌</div>
                                    <div class="right"><span>{{ item.spcd || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">批号</div>
                                    <div class="right"><span>{{ item.spbh || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">柜号</div>
                                    <div class="right"><span>{{ item.spgh || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">件数</div>
                                    <div class="right"><span>{{ item.spjs }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">单重（KG）</div>
                                    <div class="right"><span>{{ item.spjsl }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">重量（吨）</div>
                                    <div class="right"><span>{{ item.spsl }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">业务合同</div>
                                    <div class="right"><span class="red-font">{{ item.contractNo || '--' }}</span></div>
                                </div>
                                <div class="row" v-if="detail.mainInfo.bizType == 'FOREIGN_TRANS'">
                                    <div class="left">估值单价</div>
                                    <div class="right"><span>{{ item.price }}</span>元/吨</div>
                                </div>
                                <div class="row" v-if="detail.mainInfo.bizType == 'FOREIGN_TRANS'">
                                    <div class="left">估值总额</div>
                                    <div class="right"><span>{{ item.spje }}</span>元</div>
                                </div>
                                <div class="row">
                                    <div class="left">条码备注</div>
                                    <div class="right"><span class="red-font">{{ item.itbz || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">货物条码</div>
                                    <div class="right"><span>{{ item.sptm }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">旧货权人</div>
                                    <div class="right"><span>{{ item.khmc }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">新货权人</div>
                                    <div class="right"><span>{{ item.khmcn }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">库存类型</div>
                                    <div class="right"><span>{{ item.spdm }}</span></div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- 解押 -->
                <div v-if="layouType == 3">
                    <div class="info">
                        <div class="row">
                            <div class="left">解押件数</div>
                            <div class="right"><span>{{ detail.info.num }}件</span></div>
                        </div>
                        <div class="row">
                            <div class="left">解押重量</div>
                            <div class="right"><span>{{ detail.info.weight }}吨</span></div>
                        </div>
                        <div class="row">
                            <div class="left">解押货值</div>
                            <div class="right"><span>{{ detail.info.sjAmount }}元</span></div>
                        </div>
                        <div class="row">
                            <div class="left">应回款</div>
                            <div class="right"><span class="red-font">{{ detail.info.sjAmount }}元</span></div>
                        </div>
                        <div class="row">
                            <div class="left">是否出仓</div>
                            <div class="right"><span>{{ detail.info.isout == 1 ? '是' : '否' }}</span></div>
                        </div>
                    </div>
                    <div class="list" v-if="detail.list.length">
                        <div v-for="(item, index) in detail.list" :key="item.id">
                            <div class="title display-flex justify-between">
                                <span v-if="index === 0" class="text-bold font-size-26">货物信息</span>
                                <div class="text-center flex-item font-size-26"
                                    :style="index === 0 ? 'margin-left:-50px' : ''">
                                    {{
            index + 1 }}/{{ detail.list.length }}</div>
                            </div>
                            <div class="item">
                                <div class="row">
                                    <div class="left">物品规格</div>
                                    <div class="right"><span>{{ item.spzs || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">品牌</div>
                                    <div class="right"><span>{{ item.spcd || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">批号</div>
                                    <div class="right"><span>{{ item.spbh || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">柜号</div>
                                    <div class="right"><span>{{ item.spgh || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">货物条码</div>
                                    <div class="right"><span>{{ item.sptm || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">解押件数</div>
                                    <div class="right"><span>{{ item.spjs }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">单重（KG）</div>
                                    <div class="right"><span>{{ item.spjsl }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">解押重量（吨）</div>
                                    <div class="right"><span>{{ item.spsl }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">合同号</div>
                                    <div class="right"><span class="red-font">{{ item.contractNo || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">估值单价</div>
                                    <div class="right"><span>{{ item.price }}元/吨</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">解押货值</div>
                                    <div class="right"><span>{{ item.jyAmount }}元</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">支付比例</div>
                                    <div class="right"><span>{{ item.ratio }}%</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">手续单价</div>
                                    <div class="right"><span>{{ item.commissionPrice }}元/吨</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">手续总价</div>
                                    <div class="right"><span>{{ item.commissionSubtotal }}元</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">应回款</div>
                                    <div class="right"><span class="red-font">{{ item.sjAmount }}元</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">库存类型</div>
                                    <div class="right"><span>{{ item.spdm }}</span></div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- 代购、担保 -->
                <div v-if="layouType == 4">
                    <div class="info">
                        <div class="row">
                            <div class="left">担保金额</div>
                            <div class="right"><span>{{ JG.info.jine || 0 }}元</span></div>
                        </div>
                        <div class="row">
                            <div class="left">锁仓线</div>
                            <div class="right"><span>{{ JG.info.scx || 0 }}元</span></div>
                        </div>
                        <div class="row">
                            <div class="left">占用资金</div>
                            <div class="right"><span>{{ JG.info.occupationAmount || 0 }}元</span></div>
                        </div>
                        <div class="row">
                            <div class="left">本次审批货物</div>
                            <div class="right"><span>{{ JG.info.dgspsl || 0 }}吨</span></div>
                        </div>
                        <div class="row">
                            <div class="left">本次审批货值</div>
                            <div class="right"><span>{{ JG.info.dgamount || 0 }}元</span></div>
                        </div>
                        <div class="row">
                            <div class="left">需回本金</div>
                            <div class="right"><span class="red-font">{{ JG.info.hkAmount || 0 }}元</span></div>
                        </div>
                        <div class="row"
                            v-if="detail.mainInfo.bizType === 'BUY_OUT' || detail.mainInfo.bizType === 'BUY_TRANS'">
                            <div class="left">手续费</div>
                            <div class="right"><span>{{ JG.info.handlingfees || 0 }}元</span></div>
                        </div>
                        <div class="row"
                            v-if="detail.mainInfo.bizType === 'BUY_OUT' || detail.mainInfo.bizType === 'BUY_TRANS'">
                            <div class="left">需回款总计</div>
                            <div class="right"><span class="red-font">{{ JG.info.totalAmount || 0 }}元</span></div>
                        </div>
                    </div>
                    <div class="list" v-if="detail.list.length">
                        <div v-for="(item, index) in detail.list" :key="item.id">
                            <div class="title display-flex justify-between">
                                <span v-if="index === 0" class="text-bold font-size-26">货物信息</span>
                                <div class="text-center flex-item font-size-26"
                                    :style="index === 0 ? 'margin-left:-50px' : ''">
                                    {{
            index + 1 }}/{{ detail.list.length }}</div>
                            </div>
                            <div class="item">
                                <div class="row">
                                    <div class="left">物品规格</div>
                                    <div class="right"><span>{{ item.spzs || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">品牌</div>
                                    <div class="right"><span>{{ item.spcd || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">批号</div>
                                    <div class="right"><span>{{ item.spbh || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">柜号</div>
                                    <div class="right"><span>{{ item.spgh || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">货物条码</div>
                                    <div class="right"><span>{{ item.sptm || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">库存类型</div>
                                    <div class="right"><span>{{ item.spdm || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">件数</div>
                                    <div class="right"><span>{{ item.spjs }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">单重（KG）</div>
                                    <div class="right"><span>{{ item.spjsl }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">重量（吨）</div>
                                    <div class="right"><span>{{ item.spsl }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">合同号</div>
                                    <div class="right"><span class="red-font">{{ item.contractNo || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">估值单价</div>
                                    <div class="right"><span>{{ item.price }}</span>元/吨</div>
                                </div>
                                <div class="row">
                                    <div class="left">估值总额</div>
                                    <div class="right"><span>{{ item.spje }}</span>元</div>
                                </div>
                                <div class="row"
                                    v-if="detail.mainInfo.bizType === 'BUY_OUT' || detail.mainInfo.bizType === 'BUY_TRANS'">
                                    <div class="left">手续单价</div>
                                    <div class="right"><span>{{ item.handlingfees }}元/吨</span></div>
                                </div>
                                <div class="row"
                                    v-if="detail.mainInfo.bizType === 'BUY_OUT' || detail.mainInfo.bizType === 'BUY_TRANS'">
                                    <div class="left">手续总价</div>
                                    <div class="right"><span>{{ item.totalHandlingfees }}元</span></div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- 外贸货物解冻 -->
                <div v-if="layouType == 5">
                    <div class="info">
                        <div class="row">
                            <div class="left">申请单号</div>
                            <div class="right"><span>{{ detail.info.billNo }}</span></div>
                        </div>
                        <div class="row">
                            <div class="left">意向客户</div>
                            <div class="right"><span>{{ detail.info.buyerName }}</span></div>
                        </div>
                        <div class="row">
                            <div class="left">货权人</div>
                            <div class="right"><span>{{ detail.info.ownerName }}</span></div>
                        </div>
                        <div class="row">
                            <div class="left">备注</div>
                            <div class="right"><span>{{ detail.info.remark }}</span></div>
                        </div>
                    </div>
                    <div class="list" v-if="detail.list.length">
                        <div v-for="(item, index) in detail.list" :key="item.id">
                            <div class="title display-flex justify-between">
                                <span v-if="index === 0" class="text-bold font-size-26">货物信息</span>
                                <div class="text-center flex-item font-size-26"
                                    :style="index === 0 ? 'margin-left:-50px' : ''">
                                    {{
            index + 1 }}/{{ detail.list.length }}</div>
                            </div>
                            <div class="item">
                                <div class="row">
                                    <div class="left">物品规格</div>
                                    <div class="right"><span>{{ item.spzs || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">品牌</div>
                                    <div class="right"><span>{{ item.spcd || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">批号</div>
                                    <div class="right"><span>{{ item.spbh || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">柜号</div>
                                    <div class="right"><span>{{ item.spgh || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">货物条码</div>
                                    <div class="right"><span>{{ item.sptm || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">库存类型</div>
                                    <div class="right"><span>{{ item.spdm || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">件数</div>
                                    <div class="right"><span>{{ item.num }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">单重（KG）</div>
                                    <div class="right"><span>{{ item.spjsl }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">重量（吨）</div>
                                    <div class="right"><span>{{ item.weight }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">合同号</div>
                                    <div class="right"><span class="red-font">{{ item.contractNo || '--' }}</span></div>
                                </div>
                                <div class="row">
                                    <div class="left">估值单价</div>
                                    <div class="right"><span>{{ item.estimate*1000 }}</span>元/吨</div>
                                </div>
                                <div class="row">
                                    <div class="left">估值总额</div>
                                    <div class="right"><span>{{ item.estimateValue }}</span>元</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <van-field v-model="detail.remark" v-if="detail.mainInfo.needApprove == 1" rows="2" autosize
                    label="审批意见" type="textarea" maxlength="300" style="border-radius: 12px;" placeholder="请输入审批意见"
                    show-word-limit />
                <div class="btn-box">
                    <div class="btn btn1" @click="handleAudit"
                        v-if="detail.mainInfo.needApprove == 1 && (detail.mainInfo.instStatus == 1 || detail.mainInfo.instStatus == 2)">
                        审核通过</div>
                    <div class="btn btn2" @click="handleReject"
                        v-if="detail.mainInfo.needApprove == 1 && detail.mainInfo.instStatus == 1">驳回</div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import { useRoute } from 'vue-router'
import { msgError, msgSuccess, msgConfirm } from '@/utils/message'
import { setToken, setExpiresIn, getOpenId, setOpenId } from '@/utils/auth'
import { h5login, getStaffUsersListByOpenId, getOpenIdByCode, getConfigKey } from '@/api/staffUser'
import { setLocalStorage, getLocalStorage } from '@/utils/localStorage'
import { storageWxOApproveStorage, storageApproveInstApprove, storageApproveInstReject, storageApproveInstGetDBimage } from '@/api/approve'
import { WEIGHT_DIGIT, NUMBER_DIGIT, AMOUNT_DIGIT } from '@/utils/constant'
import type { Ref } from 'vue'

const route = useRoute();
const detail: any = reactive({
    id: route.query.instId || '',
    loading: false,
    mainInfo: {
        approveNo: '',
        bizType: '',
        bizTypeName: '',
        allname: '',
        createBy: '',
        spjs: '',
        spsl: '',
        spckmc: '',
        rqid: '',
        statusName: '',
        needApprove: '',
        instStatus: ''
    },
    info: {
        outDate:""
    },
    list: [{}],
    remark: ''
})

let showPage: Ref<number> = ref(0);
const configKeyParam: string = 'GZH_APP_ID'
let wxAppid: string = ''
const code: any = route.query.code;
//是否已调用微信返回的code,如果调用wxCode则为1，否则为0
const wxCode: number = getLocalStorage('wxCode') == 1 ? 1 : 0;
const openId: any = getOpenId();
const staffUserListData: any = reactive({
    list: []
})
let layouType: Ref<number> = ref(0);
let JG: any = reactive({
    info: {
        dgspsl: '',
        dgamount: '',
        hkAmount: '',
        handlingfees: '',
        totalAmount: ''
    }
})

onMounted(() => {
    if (openId) {
        staffUsersListByOpenId(openId);
    } else {
        if (code) {
            if (wxCode == 0) {
                const href = window.location.href.split('?')[0]
                return configKey(href)
            }
            if (wxCode == 1) {
                setLocalStorage('wxCode', 0)
                return openIdByCode(code)
            }
        } else {
            configKey(window.location.href)
        }
    }
})

function configKey(redirectUri: string) {
    getConfigKey(configKeyParam).then((res: any) => {
        wxAppid = res.msg;
        getCode(redirectUri);
    })
}
function getCode(redirectUri: string) {
    setLocalStorage('wxCode', 1)
    let param = "appid=" + wxAppid + "&redirect_uri=" + redirectUri + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect" + "&id=" + route.query.id;
    let url = "https://open.weixin.qq.com/connect/oauth2/authorize?" + param;
    window.location.href = url
}

function openIdByCode(code: string) {
    getOpenIdByCode({ code: code }).then((res: any) => {
        const openId: string = res.msg;
        setOpenId(openId);
        staffUsersListByOpenId(openId);
    })
}

function staffUsersListByOpenId(openId: string) {
    getStaffUsersListByOpenId({ openId: openId }).then((res: any) => {
        const data = res.data;
        staffUserListData.list = data;
        if (data && data.length == 0) {
            return showPage.value = 1;
        }
        if (data && data.length == 1) {
            login(0)
        }
        if (data && data.length > 1) {
            return showPage.value = 2;
        }
    })
}

function login(index: number) {
    const data: any = staffUserListData.list
    h5login({ username: data[index].userName, password: data[index].password }).then((res: any) => {
        const data1 = res.data;
        setToken(data1.access_token);
        setExpiresIn(data1.expires_in);
        getDetail();
    })
}

function getDetail() {
    storageWxOApproveStorage(detail.id).then((res: any) => {
        console.log("detail数据",res);
        showPage.value = 3;
        let data = res.data;
        if (data.inst) {
            const bizType = data.inst.bizType;
            let list = [];
            if (data.detailInfo && data.detailInfo.list && data.detailInfo.list.length) {
                list = handleListPrice(bizType, data.detailInfo.list);
            }
            detail.list = list;
            data.inst.statusName = getInstStatusName(data.inst.instStatus);
            data.inst.bizTypeName = getBizTypeName(bizType);
            detail.mainInfo = data.inst;
            if(detail.mainInfo.bizType == "FOREIGN_THAW"){
                console.log("当前解冻");
                
                detail.info = data.detailInfo.stockThawFreeze;
            }else{
                console.log("当前非解冻");
                detail.info = data.detailInfo.info;
                detail.info.outDate = data.inst.rqid;
            }
            getLayout();
        }
        detail.loading = false;
    })
}

function handleListPrice(bizType: string, list: any[]) {
    switch (bizType) {
        case 'FOREIGN_OUT':
        case 'FOREIGN_TRANS':
            list.forEach((item: any) => {
                item.price = item.estimate ? item.estimate : '';
                item.spje = item.price ? Number((item.price * item.spsl).toFixed(AMOUNT_DIGIT)) : '';
            })
            break;
        case 'JG_OUT':
        case 'JG_OUT_URGENT':
        case 'JG_TRANS':
        case 'JG_TRANS_URGENT':
            list.forEach((item: any) => {
                item.price = item.gjdj ? item.gjdj : '';
                item.spje = item.price ? Number((item.price * item.spsl).toFixed(AMOUNT_DIGIT)) : '';
            })
            break;
        case 'BUY_OUT':
        case 'BUY_TRANS':
            list.forEach((item: any) => {
                item.price = item.procurePrice ? item.procurePrice : '';
                item.spje = item.price ? Number((item.price * item.spsl).toFixed(AMOUNT_DIGIT)) : '';
                item.totalHandlingfees = item.handlingfees ? (item.handlingfees * item.spsl).toFixed(AMOUNT_DIGIT) : 0;
            })
            break;
    }
    return list;
}

function getLayout() {
    let result = 0;
    switch (detail.mainInfo.bizType) {
        case 'OUT':
        case 'FOREIGN_OUT':
            result = 1;
            break;
        case 'TRANS':
        case 'FOREIGN_TRANS':
            result = 2;
            break;
        case 'JS_OUT':
        case 'JS':
            result = 3;
            break;
        // case 'JG_OUT':
        case 'JG_OUT_URGENT':
        // case 'JG_TRANS':
        case 'JG_TRANS_URGENT':
        case 'BUY_OUT':
        case 'BUY_TRANS':
            result = 4;
            getJGInfo();
            break;
        case 'FOREIGN_THAW':
            result = 5;
            break;
    }
    layouType.value = result;
}

function getBizTypeName(type: String) {
    let name = '';
    switch (type) {
        case 'OUT':
            name = '普通出仓';
            break;
        case 'TRANS':
            name = '普通转货';
            break;
        case 'JS_OUT':
            name = '解押出仓';
            getSumList();
            break;
        case 'JS':
            name = '解押';
            getSumList();
            break;
        case 'FOREIGN_OUT':
            name = '外贸货物出仓';
            break;
        case 'FOREIGN_TRANS':
            name = '外贸货物转货';
            break;
        case 'JG_OUT':
            name = '担保出仓';
            break;
        case 'JG_OUT_URGENT':
            name = '担保应急出仓';
            break;
        case 'JG_TRANS':
            name = '担保转货';
            break;
        case 'JG_TRANS_URGENT':
            name = '担保应急转货';
            break;
        case 'BUY_OUT':
            name = '代购出仓';
            break;
        case 'BUY_TRANS':
            name = '代购转货';
            break;
        case 'FOREIGN_THAW':
            name = '外贸货物解冻';
            break;
    }
    return name;
}

function getInstStatusName(status: String) {
    let name = '';
    switch (status) {
        case '1':
            name = '审批中';
            break;
        case '2':
            name = '已驳回';
            break;
        case '3':
            name = '已完成';
            break;
        case '4':
            name = '已作废';
            break;
    }
    return name;
}

function getSumList() {
    const dts = detail.list;
    let num = 0
    let weight = 0
    let jyAmount = 0
    let sjAmount = 0
    for (let i in dts) {
        const item = dts[i]
        item.num = item.num || 0
        item.weight = item.weight || 0
        item.jyAmount = item.jyAmount || 0
        num += Number(item.num)
        weight += Number(item.weight)
        jyAmount += Number(item.jyAmount)
        sjAmount += Number(item.sjAmount)
    }
    num = parseFloat(num.toFixed(NUMBER_DIGIT));
    weight = parseFloat(weight.toFixed(WEIGHT_DIGIT));
    jyAmount = parseFloat(jyAmount.toFixed(AMOUNT_DIGIT));
    sjAmount = parseFloat(sjAmount.toFixed(AMOUNT_DIGIT));
    let sumInfo = {
        num, weight, jyAmount, sjAmount
    }
    let info = JSON.parse(JSON.stringify(detail.info));
    detail.info = {
        ...info,
        ...sumInfo
    }
}

function getJGInfo() {
    storageApproveInstGetDBimage(detail.info.dzid).then(res => {
        let JGlist = res.data || [];
        if (JGlist && JGlist.length) {
            JG.info = JGlist[0];
        }
    })
}

//审核通过
function handleAudit() {
    let data = {
        instId: detail.id,
        remark: detail.remark
    }
    msgConfirm("是否确认此操作？").then(() => {
        storageApproveInstApprove(data).then(() => {
            msgSuccess('审核通过！');
            detail.mainInfo.needApprove = '0';
            detail.mainInfo.instStatus = '3';
            getInstStatusName('3');
        })
    })
}

// 驳回
function handleReject() {
    if (!detail.remark) return msgError('请输入驳回原因！');
    let data = {
        instId: detail.id,
        remark: detail.remark
    }
    msgConfirm("是否确认此操作？").then(() => {
        storageApproveInstReject(data).then(() => {
            detail.mainInfo.needApprove = '0';
            detail.mainInfo.instStatus = '2';
            getInstStatusName('2');

        })
    })

}

</script>

<style lang="scss" scoped>
div,
span {
    box-sizing: border-box;
}

.empty,
.unbound-tip {
    width: 100%;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
}

.approve-detail {
    width: 100%;
    background-color: #F2F2F2;
    min-height: 100vh;

    .approve-detail-container {
        padding: 24px;
        width: 100%;

        .item {
            margin: 30px 0;
        }

        .info,
        .item {
            padding: 40px 40px;
            background-color: #fff;
            border-radius: 32px;
            overflow: hidden;
            position: relative;

            &:not(:last-child) {
                margin-bottom: 24px;
            }

            .tag {
                width: 104px;
                height: 50px;
                line-height: 50px;
                text-align: center;
                border-radius: 0px 32px 0px 32px;
                font-size: 24px;
                font-weight: 500;
                position: absolute;
                top: 0;
                right: 0;
            }

            .tag-0 {
                background: #FFEBE3;
                color: #F17844;
            }

            .tag-1 {
                background: #dbf5ec;
                color: #2FB17C;
            }

            .item-main {
                .item-main-row {
                    margin-bottom: 16px;
                    font-weight: 500;
                    color: #000000;
                    display: flex;

                    .left {
                        width: 300px;
                    }

                    .right {}
                }
            }

            .line {
                margin: 24px 0;
                border: 1px solid rgba(209, 209, 209, 0.1);
            }

            .row {
                font-size: 30px;
                display: flex;

                &:not(:last-child) {
                    margin-bottom: 16px;

                }

                .left {
                    width: 200px;
                    color: #7e7e7e;
                }

                .right {
                    color: #000000;

                    .red-font {
                        color: #CD4C46;
                    }
                }
            }
        }

        .btn-box {
            margin-top: 44px;

            .btn {
                margin: 24px auto 0;
                width: 690px;
                height: 96px;
                line-height: 96px;
                text-align: center;
                border-radius: 48px;
            }

            .btn1 {
                color: #FFFFFF;
                background: #2386E3;
            }

            .btn2 {
                color: #333333;
                background: #FFFFFF;
                border: 2rpx solid #DDDDDD;
            }
        }
    }
}
</style>